{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cf46ec61-7914-4677-b12b-a9e478e88d3f",
   "metadata": {},
   "source": [
    "# Credit Risk Cleanup"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ae8aaec-e01d-48d3-b768-98661ad1ec85",
   "metadata": {},
   "source": [
    "Run this notebook if you are done experimenting with this demo, or if you wish to start again with a clean slate.\n",
    "\n",
    "**RUNNING THE FOLLOWING CODE WILL REMOVE FILES AND PROCESSES CREATED BY THE PREVIOUS EXAMPLE NOTEBOOKS.**\n",
    "\n",
    "The notebook progresses in reverse order of how the files and processes were added. (The reverse order makes it possible to partially revert changes by running cells up to a certain point.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6feaa771-4226-459f-b6dd-214024cb5c7c",
   "metadata": {},
   "source": [
    "#### Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "20a39e94-920d-4108-aa6b-1e29d2224f71",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports\n",
    "import os\n",
    "import time\n",
    "import psutil"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f124260-a8b2-475d-9103-8d336c543fce",
   "metadata": {},
   "source": [
    "#### Remove Trained Model File"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7a05a2b-9a26-4722-a526-84da99fc0b29",
   "metadata": {},
   "source": [
    "This removes the model that was created and saved in [03_Credit_Risk_Model_Training.ipynb](03_Credit_Risk_Model_Training.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a6b21063-ea43-4329-be0c-c1644c705db2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Remove the model file that was saved in model training.\n",
    "model_path = \"./rf_model.pkl\"\n",
    "os.remove(model_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed97c24a-8f25-4e77-9037-f9cf4ad68dfa",
   "metadata": {},
   "source": [
    "#### Shutdown Servers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f825d10-c13d-4701-b102-e15ad1c0bd3b",
   "metadata": {},
   "source": [
    "Shut down the servers that were launched in [02_Deploying_the_Feature_Store.ipynb](02_Deploying_the_Feature_Store.ipynb); also remove the `server_proc.txt` that held the process PIDs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "66db4d46-a895-4041-ad87-ab0a77f13211",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load server process objects\n",
    "server_pids = open(\"server_proc.txt\").readlines()\n",
    "offline_server_proc = psutil.Process(int(server_pids[0].strip()))\n",
    "online_server_proc = psutil.Process(int(server_pids[1].strip()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "001fd472-2e28-499e-9eac-0a16ad8187a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Online server :            psutil.Process(pid=44621, name='python3.11', status='running', started='14:19:05')\n",
      "Online server is running:  True\n",
      "\n",
      "Offline server PID:        psutil.Process(pid=44594, name='python3.11', status='running', started='14:19:03')\n",
      "Offline server is running: True\n"
     ]
    }
   ],
   "source": [
    "# Verify if servers are running\n",
    "def verify_servers():\n",
    "    # online server\n",
    "    print(f\"Online server :            {online_server_proc}\")\n",
    "    print(f\"Online server is running:  {online_server_proc.is_running()}\", end='\\n\\n')\n",
    "    # offline server\n",
    "    print(f\"Offline server PID:        {offline_server_proc}\")\n",
    "    print(f\"Offline server is running: {offline_server_proc.is_running()}\")\n",
    "    \n",
    "verify_servers()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "68376350-790a-4e7e-9325-c7de4d22e54b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Terminate offline server\n",
    "offline_server_proc.terminate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "446b6bf9-aef2-4873-b477-8bf595a8eabf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Terminate online server (master and worker)\n",
    "for child in online_server_proc.children(recursive=True):\n",
    "    child.terminate()\n",
    "online_server_proc.terminate()\n",
    "time.sleep(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "774827f6-4dcd-495b-b5c5-186b97148619",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Online server :            psutil.Process(pid=44621, name='python3.11', status='terminated', started='14:19:05')\n",
      "Online server is running:  False\n",
      "\n",
      "Offline server PID:        psutil.Process(pid=44594, name='python3.11', status='terminated', started='14:19:03')\n",
      "Offline server is running: False\n"
     ]
    }
   ],
   "source": [
    "# Verify termination\n",
    "verify_servers()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f8a155e4-23b3-4fb3-b868-02ba2e0a4a31",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Remove server_proc.txt (file for keeping track of pids)\n",
    "os.remove(\"server_proc.txt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed7d6f25-d255-4986-9cf2-9876f6c558cc",
   "metadata": {},
   "source": [
    "#### Remove Feast Applied Configuration Files"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d73efe15-a1d9-459b-8142-835dc2bf1c9f",
   "metadata": {},
   "source": [
    "Remove the registry and online store (SQLite) files created on`feast apply` created in [02_Deploying_the_Feature_Store.ipynb](02_Deploying_the_Feature_Store.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0f13a4ac-d2ad-462b-b65e-4266b7cb4922",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.remove(\"Feature_Store/data/online_store.db\")\n",
    "os.remove(\"Feature_Store/data/registry.db\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb0494cd-0143-4f5f-b7d6-9675e1403d9f",
   "metadata": {},
   "source": [
    "#### Remove Feast Configuration Files"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86c33ac7-9e1f-4798-9f14-77773a1c13bd",
   "metadata": {},
   "source": [
    "Remove the configution and feature definition files created in [02_Deploying_the_Feature_Store.ipynb](02_Deploying_the_Feature_Store.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a747043f-05fe-4b44-979d-9b30565074ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.remove(\"Feature_Store/feature_store.yaml\")\n",
    "os.remove(\"Feature_Store/feature_definitions.py\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81975a0f-7fd6-4ed3-91cf-812946df4713",
   "metadata": {},
   "source": [
    "#### Remove Data Files"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8182dc1e-d5c1-4739-b7c7-0620e93c5b64",
   "metadata": {},
   "source": [
    "Remove the data files created in [01_Credit_Risk_Data_Prep.ipynb](01_Credit_Risk_Data_Prep.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4ddb4fb2-fea1-4b70-8978-732af9a1cd3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "for f in [\"data_a.parquet\", \"data_b.parquet\", \"labels.parquet\"]:\n",
    "    os.remove(f\"Feature_Store/data/{f}\")\n",
    "os.rmdir(\"Feature_Store/data\")\n",
    "os.rmdir(\"Feature_Store\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
